<template>
  <div class="qingwu">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ $t('bank.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="el-icon-back" @click="$router.go(-1)">
              {{ $t('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main color">
        <el-form label-width="100px" ref="dataForm" :model="dataForm" :rules="dataRule">

          <el-form-item :label="$t('bank.logo')" prop="logo">
            <el-upload class="avatar-uploader" :action="this.$http.adornUrl('/file/picture')" :show-file-list="false" :headers="upload_headers" :on-success="handleLogoSuccess" :before-upload="beforePictureUpload">
              <img v-if="dataForm.logo" :src="dataForm.logo" class="avatar-upload">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="red">
              上传图片分辨率为：50*50
            </div>
          </el-form-item>

          <el-form-item :label="$t('bank.code')" prop="code">
            <el-input :placeholder="$t('common.please_input')+$t('bank.code')" v-model="dataForm.code"></el-input>
          </el-form-item>

          <el-form-item :label="$t('bank.name')" prop="name">
            <el-input :placeholder="$t('common.please_input')+$t('bank.name')" v-model="dataForm.name"></el-input>
          </el-form-item>

          <el-form-item :label="$t('bank.color')" prop="color">
            <el-input :placeholder="$t('common.please_input')+$t('bank.color')" v-model="dataForm.color"></el-input>
          </el-form-item>

          <el-form-item :label="$t('common.sort')" prop="sort">
            <el-input-number :placeholder="$t('common.please_input')+$t('common.sort')" v-model="dataForm.sort"></el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('module:common:bank:handle')" type="primary" @click="dataFormSubmit()">
              {{ $t('common.confirm') }}
            </el-button>
            <el-button @click="resetForm()">
              {{ $t('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import common from '@/views/common/base'
  export default {
    extends: common,
    data() {
      return {
        model: 'common/bank',
        upload_headers:{},
        dataForm:
        {
          id: 0,
          logo: '',
          code: '',
          name: '',
          color: '',
          sort: 0,
        },
        dataRule:
        {
          logo: [
            { required: true, message: this.$t('bank.rules.logo.require'), trigger: 'blur' },
          ],
          name: [
            { required: true, message: this.$t('bank.rules.name.require'), trigger: 'blur' },
          ]
        }
      };
    },
    methods: {
      init ()
      {
        let id = this.$route.query.id

        this.dataForm.id = id || 0
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/common/bank/view/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.status === 200) {
                this.dataForm.logo = data.data.logo
                this.dataForm.code = data.data.code
                this.dataForm.name = data.data.name
                this.dataForm.color = data.data.color
                this.dataForm.sort = data.data.sort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/common/bank/handle`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'logo': this.dataForm.logo,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'color': this.dataForm.color,
                'sort': this.dataForm.sort,
              })
            }).then(({data}) => {
              if (data && data.status === 200) {
                this.$message.success(this.$t('common.handle_success'));
                this.$router.go(-1);
              } else {
                this.$message.error(this.$t(data.message))
              }
            })
          }
        })
      },
      resetForm:function()
      {
        this.$refs['dataForm'].resetFields();
      }
    },
    created() {
      this.init();

      // 要保证取到
      this.upload_headers.Authorization = 'Bearer ' + localStorage.getItem('token');
    }
  };
</script>
